﻿import json
from flask import request

successRet = {"type": "success"}
errorRet = {"type": "error"}

from functools import wraps
from flask import request

def Deco(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        try:
            postData = json.loads(request.get_data().decode("utf-8"))
        except:
            postData = {}
        
        # try: # 为了调试方便先这样
        if request.method == 'POST': # GET 方法不需要传入 postData
            t = func(postData, *args, **kwargs)
        else:
            t = func(*args, **kwargs)
        assert isinstance(t, tuple) and len(t) >= 2
        
        stt = t[0] # func 返回的第一个值一定是操作状态
        ret = {"type": "success" if stt else "error"} # 根据操作状态选择返回值
        ret["info"] = t[1] # 返回的第二个值一定是提示语
        if len(t) == 3: ret.update(t[2]) # 如果有第三个参数那么就是返回的内容
        print("Deco RET =", ret)
        # except:
        #     ret = errorRet
        #     ret["info"] = "未知错误"
        
        return json.dumps(ret)
    return wrapper